<?php
class user extends base{

	public function init(){
		parent::init();

		$this->userObj = D('user');

		$this->adminObj = D('company_admin');
		$this->orderObj = D('company_order');
		$this->order_logObj = D('company_order_log');

		$this->upload_fileObj = D('upload_file');
		// $this->helpObj = D('help');

		$this->userObj = D('sys_user');

	}

	public function login()
    {
		$userinfo = $this->formData('formData');
		$arrBase = array(
			// 'wxapp_id' => $this->wxapp_id,
			'userinfo' => $userinfo,
			'code' => arg('code'),
		);
		$data['user_id'] = $this->userObj->login($arrBase,$this->setting['wxapp']);
		$data['user_token'] = $this->userObj->get_token();
		
		$this->renderSuccess('登录成功','',$data);
	}
	
    public function detail()
    {
		$data['user'] = $this->user;
		$this->renderSuccess('获取成功','',$data);
	}
	

	public function stat()
	{
		// 当前用户信息
		$userInfo = $this->getUser();

		$cond = "status=1 ".$this->cond_base;

		//权限
		switch($this->user['level']){
			case '0'://管理员
				$cond .=" ";
				break;
			case '1'://报修员
				$cond .=" and admin_id={$this->user_id}";
				break;
			case '2'://报修组长
				$cond .=" ".$this->cond_cate.$this->cond_zone;
				break;
			case '3'://维修师傅
				$cond .=" and shifu_id={$this->user_id}";
				break;
			case '4'://维修组长
				$cond .=" ".$this->cond_cate.$this->cond_zone;
				break;
		}
		//总订单
		$cond_sql0 = $cond . "";

		//今日订单
		$day1 = date("Y-m-d");
		$cond_sql1 = $cond . " and (add_time>='{$day1}')";

		//昨日订单
		$day1 = date("Y-m-d");
		$day2 = date("Y-m-d",strtotime("-1 day"));
		$cond_sql2 = $cond . " and (add_time>='{$day1}' and add_time<='{$day2}')";
		//本周订单
		// $day1 = date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600));
		// $cond_sql2 = $cond . " and (add_time>='{$day1}')";
		//本月订单
		$day1 = date("Y-m-01");
		$cond_sql3 = $cond . " and (add_time>='{$day1}')";
		//上月订单
		$day1 = date('Y-m-d', strtotime('-1 month', strtotime(date('Y-m', time()) . '-01 00:00:00')));
		$day2 = date('Y-m-d', strtotime(date('Y-m', time()) . '-01 00:00:00') - 86400);
		$cond_sql4 = $cond . " and (add_time>='{$day1}' and add_time<='{$day2}')";
		
		//统计
		$stat['order0'] = $this->orderObj->findCount(array($cond_sql0));
		$stat['order1'] = $this->orderObj->findCount(array($cond_sql1));
		$stat['order2'] = $this->orderObj->findCount(array($cond_sql2));
		$stat['order3'] = $this->orderObj->findCount(array($cond_sql3));
		$stat['order4'] = $this->orderObj->findCount(array($cond_sql4));

		$this->stat = $stat;
		$data['stat'] = $this->stat;
		$this->renderSuccess('获取成功','',$data);

	}


	public function userinfo()
    {
		if($this->admin){
			$data['user'] = $this->admin;

			$item[0] = [0,'','已接单'];
			$item[1] = [0,'','维修中'];
			$item[2] = [0,'','已完成'];
			$item[3] = [$this->admin['score'],'','积分'];
		}else{
			$item[0] = [0,'','已接单'];
			$item[1] = [0,'','维修中'];
			$item[2] = [0,'','已完成'];
			$item[3] = [0,'','积分'];
		}

		$data['items'] = $item;

        return $this->renderSuccess('获取成功','',$data);
    }

	public function setting(){
		$formData = $this->formData('formData');
		$arrBase = array(
			'realname' => $formData['realname'],
			// 'tel' => $formData['tel'],
			'weixin'=>$formData['weixin'],
		);
		$this->userObj->edit($this->user['id'], $arrBase);
		$this->renderSuccess('更新成功','',$formData);
	}


	public function pwd(){

		if($this->fmdo == 'pwd'){
			$arrBase = array(
				'password' => qcing_hash(arg('pwd')),
			);
			$this->modelObj->edit($this->user_id, $arrBase);
			$this->ajaxMsg(1,'修改成功');
		}
	}
	

	public function score_log(){

		$arrBase = array('tab' => $this->tab,'page' => '0000',);
		if($this->keyword){
			$arrBase = array_merge($arrBase,array('keyword' => $this->keyword));
		}
		$this->nextpage = url($this->routeUrl,$this->action.'_loaddata', $arrBase);
		
	}

	//loaddata
	public function score_log_loaddata(){
		
		$cond = "status=1 and user_id={$this->user_id}";
		//搜索
		if($this->keyword){
			$cond.=" and (msg like '%".$this->keyword."%' ) ";
		}

		$result = $this->score_logObj->get_loaddata($cond);
		if($result){
			$this->list = $result;
		}else{
			die('0');
		}
	}



	public function payno(){
        if($this->user){

            $no = arg('no');
            // $level = mb_strlen($no);

			$payno = make_orderno();
			// $paymoney = $this->cardObj->get_card_money(0,$level);

            $data['user_id'] = $this->user['id'];
			$data['payno'] = $payno;
			$data['money'] = 0.01;
            // $data['card_no'] = $no;
            $data['msg'] = "购买卡片 NO:{$no}";

            $log_id = $this->pay_logObj->add_log($data);
            if($log_id){
				$open_id = $this->user['open_id'];
				
				$wxapp = $this->setting['wxapp'];
                // $wxapp = D('setting')->get_setting('gzh');

                $payment_config = array(
                    'app_id'=>$wxapp['app_id'],
                    'app_secret'=>$wxapp['app_secret'],
                    // 'mch_id'=>$wxapp['mchid'],
					// 'mch_key'=>$wxapp['apikey'],
					'mch_id'=>'1374647602',
                    'mch_key'=>'DSAD21312321EDSADSAD214RETWERWER',
                    // 'code_url'=>'https://wx.ndqc.com.cn/weixin13/index/my/pay_order?no='.$no,
                    'notify_url'=>'/index/my/pay_ok?no='.$payno,
				);

				$payment_log = $this->pay_logObj->get_info($log_id);
				$payment_log['openid'] = $open_id;
				$jssdk = D('weixin')->payment($payment_config,$payment_log);
				if($jssdk){
					$data = $jssdk;
					$data['payno'] = $payno;
					$this->renderSuccess('成功','', $data);
				}
            }else{
                $this->renderError('失败');
            }
        }else{
            $this->renderError('请先登录');
        }
    }

     //
    public function payok(){
        if($this->user){
			$payno = arg('payno');
            $log_info = $this->pay_logObj->get_info("payno='{$payno}' and status=0");
			if($log_info){
				$this->pay_logObj->up_status($log_info['id']);

				$this->renderSuccess('支付成功','', $data);
			}else{
				$this->renderError('支付错误', $res);
			}

        }
    }


	//
	public function help()
	{

		$this->helpObj = D('help');

		$info_id = arg('info_id');
		$keyword = arg('keyword');

		$cond = "status=1 ";
		if($info_id){
			$cond .=" and id={$info_id}";
		}else{
			$cond .=" and id>1";
		}
		//搜索
		if($keyword){
			$cond.=" and (title like '%".$keyword."%' or intro like '%".$keyword."%') ";
		}

		$result = $this->helpObj->getList($cond);
		if($result){
			if($info_id){
				if($this->user['id']){
					$result['title'] = $result['list'][0]['title'];
					// $result['log_id'] = $this->house_logObj->dianzan($this->type,$info_id,$this->user['id'],'访问了房源');
				}
			}
			$this->renderSuccess('获取成功','',$result);
		}else{
			$this->renderError('没有数据');
		}
	}

}